﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TfsRollUp.Core;

namespace TfsRollUp.Test
{
    /// <summary>
    /// Create a simple hierarchical scenario, wait for the rollup to be calculated and verify the answer
    /// (Everone knows its 42)
    /// </summary>
    [TestClass]
    public class WorkItemIntegrationTest
    {
        /// <summary>
        /// One simple test
        /// </summary>
        [TestMethod]
        public void TotalInUserStoryWith2ChildTasksMustBeCalculatedAsTheAnswer()
        {
            using (var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://localhost:8080/tfs/DefaultCollection")))
            {
                var store = tfs.GetService<WorkItemStore>();

                // Grab the first team project
                var project =  store.Projects[0];
                var userStoryType = project.WorkItemTypes["User Story"];
                var taskType = project.WorkItemTypes["Task"];

                var fieldCollection = new WorkItemFieldCollection(store);
                var userStoryFieldList = fieldCollection.GetRollUpFieldList(project.Name, "User Story");
                if (userStoryFieldList == null || userStoryFieldList.Length == 0)
                {
                    Assert.Fail("Theres no Alm Rollup Fields (Alm.RollUp.) in the team project:'{0}' for the 'User Story' workitem type", project.Name);
                }
                Assert.IsTrue(userStoryFieldList.Contains("Alm.RollUp.Microsoft.VSTS.Scheduling.RemainingWork"));

                var taskFieldList = fieldCollection.GetRollUpFieldList(project.Name, "Task");
                if (taskFieldList == null || taskFieldList.Length == 0)
                {
                    Assert.Fail("Theres no Alm Rollup Fields (Alm.RollUp.) in the team project:'{0}' for the 'Task' workitem type", project.Name);
                }
                Assert.IsTrue(taskFieldList.Contains("Alm.RollUp.Microsoft.VSTS.Scheduling.RemainingWork"));
                Assert.IsTrue(taskFieldList.Contains("Microsoft.VSTS.Scheduling.RemainingWork"));

                var userStory = new WorkItem(userStoryType);
                userStory.Title = Constants.TfsRollUpPlugin + ".Book";

                var task1 = new WorkItem(taskType);
                task1.Title = Constants.TfsRollUpPlugin + ".Dent";
                task1.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value = 20;
                task1.Save();

                var task2 = new WorkItem(taskType);
                task2.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value = 22;
                task2.Title = Constants.TfsRollUpPlugin + ".Prefect";
                task2.Save();

                WorkItemLinkTypeEnd linkTypeEnd = store.WorkItemLinkTypes.LinkTypeEnds["Child"];
                userStory.Links.Add(new RelatedLink(linkTypeEnd, task1.Id));
                userStory.Links.Add(new RelatedLink(linkTypeEnd, task2.Id));
                userStory.Save();
                Assert.IsTrue(userStory.Fields["Alm.RollUp.Microsoft.VSTS.Scheduling.RemainingWork"].Value == null);
                Debug.WriteLine("Come sweet slumber and shroud me in thy purple cloak. (Doesn't even rhyme!)");
                Thread.Sleep(500);

                Debug.WriteLine("Look ma, no hands");
                userStory.SyncToLatest();
                var answer = (double)userStory.Fields["Alm.RollUp.Microsoft.VSTS.Scheduling.RemainingWork"].Value;
                Assert.AreEqual("The answer is 42", "The answer is " + (int)answer);
            }
        }
    }
}
